package com.starmark.gateway.springcloud.provider.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.starmark.gateway.springcloud.provider.dto.AccessLogDto;
import com.starmark.gateway.springcloud.provider.service.ILogService;
import com.starmark.message.queue.producer.api.dto.MessageQueueDto;
import com.starmark.message.queue.producer.api.factory.MessageQueueProducerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 日志服务
 *
 * @author starmark
 * @date 20-2-6  下午3:25
 */
@Service
@Slf4j
public class LogServiceImpl implements ILogService {


    @Value("${starmark.log.access.kafka.topic:accessLog}")
    private String accessLogTopic;
    @Autowired
    private MessageQueueProducerFactory messageQueueProducerFactory;

    @Override
    @Async
    public void saveLog(AccessLogDto logDto) {

        log.info("请求日志:{}",
                logDto);
        messageQueueProducerFactory.getMessageQueueProducerService("redis")
                .produceMessage(new MessageQueueDto(accessLogTopic, JSONObject.toJSONString(logDto)));
    }
}
